import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client {

	public static void main(String[] args) throws Exception {
		new MainWindow();
	}
}

class MainWindow extends JFrame {
	final String SERVER_IP = "47.106.216.148";
	final String SERVER_PORT = "5600";
	JButton selectFile, uploadFile;
	JTextField fileDirectory, serverIP, serverPort;
	JLabel lbServerIP, lbServerPort, lbFileDirectory;

	public MainWindow() {
		this.setSize(290, 115);
		this.setResizable(false);
		this.setLayout(new FlowLayout());

		lbServerIP = new JLabel("服务器IP:");
		this.add(lbServerIP);
		serverIP = new JTextField(10);
		serverIP.setText(SERVER_IP);
		this.add(serverIP);

		lbServerPort = new JLabel("端口:");
		this.add(lbServerPort);
		serverPort = new JTextField(5);
		serverPort.setText(SERVER_PORT);
		this.add(serverPort);

		lbFileDirectory = new JLabel("文件路径:");
		this.add(lbFileDirectory);
		fileDirectory = new JTextField(19);
		this.add(fileDirectory);

		selectFile = new JButton("选择");
		selectFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new JDialog(new FileChooseWindow(fileDirectory), "选择上传的文件").setModal(true);
			}
		});
		this.add(selectFile);

		uploadFile = new JButton("上传");
		uploadFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread(new Upload(serverIP.getText(), serverPort.getText(), fileDirectory.getText())).start();
				fileDirectory.setText(null);
			}
		});
		this.add(uploadFile);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null); // 将窗口设置到屏幕中间
		this.setVisible(true);
	}
}

class FileChooseWindow extends JFrame {
	JFileChooser fc;
	JTextField fileDirectory;

	public FileChooseWindow(JTextField fileDirectory) {
		this.fileDirectory = fileDirectory;
		fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int value = fc.showDialog(null, "选择");
		// 如果是按的确认键退出窗口
		if (value == JFileChooser.APPROVE_OPTION) {
			// 判断选择的是否是一个文件
			if (fc.getSelectedFile().isFile()) {
				fileDirectory.setText(fc.getSelectedFile().getPath());
			}
		}
	}
}

class Upload implements Runnable {
	String SERVER_IP;
	int SERVER_PORT;
	String PATH;

	public Upload(String ip, String port, String path) {
		SERVER_IP = ip;
		SERVER_PORT = Integer.valueOf(port);
		PATH = path;
	}

	public void run() {
		try {
			File file = new File(PATH);
			// 上传文件名
			Socket socket = new Socket(SERVER_IP, SERVER_PORT);
			OutputStream out = socket.getOutputStream();
			FileInputStream fis = new FileInputStream(file.getPath());
			out.write((file.getName() + "|").getBytes()); // 用|来分隔数据包
			// 上传文件
			byte[] buf = new byte[1024];
			int len;
			while ((len = fis.read(buf)) != -1) {
				out.write(buf, 0, len);
			}
			socket.shutdownOutput();
			// 从服务端获得成功信息
			InputStream in = socket.getInputStream();
			len = in.read(buf);
			System.out.println(new String(buf, 0, len));
			// 关闭流
			in.close();
			out.close();
			fis.close();
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
